package com.beta.admin.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.beta.common.web.ContentType;
import com.beta.vo.home.HomeRecommendsVO;
import com.beta.admin.request.home.HomeRecommendCreateRequest;
import com.beta.admin.request.home.HomeRecommendUpdateRequest;
import com.beta.admin.support.web.BaseController;
import com.beta.dto.home.HomeRecommendsDto;
import com.beta.service.IHomeRecommendsService;
import io.swagger.annotations.Api;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by vector on 2017/2/28.
 */
@Api(tags = "HomeRecommends")
@RestController
@RequestMapping(RequestMappingConstants.MAPPING_PREFIX + "/home_recommends")
public class HomeRecommendsController extends BaseController {

    @Autowired
    private IHomeRecommendsService homeRecommendsService;

    @Autowired
    private MapperFacade beanMapper;

    @GetMapping(value = "", produces = { ContentType.JSON })
    public Object homeRecommends() {
        Page<HomeRecommendsDto> homeRecommendsPage = homeRecommendsService.getHomeRecommendsPage(getMybatisPage());
        return pageData(homeRecommendsPage, homeBannersDto -> beanMapper.map(homeBannersDto, HomeRecommendsVO.class));
    }

    @PostMapping(value = "", consumes = {ContentType.JSON}, produces = ContentType.JSON)
    public Object createHomeRecommend(@Valid @RequestBody HomeRecommendCreateRequest createRequest) {
        homeRecommendsService.createHomeRecommend(beanMapper.map(createRequest, HomeRecommendsDto.class));
        return ok();
    }

    @GetMapping(value = "/{id}", produces = { ContentType.JSON })
    public Object getHomeRecommendById(@PathVariable("id") Integer id) {
        HomeRecommendsDto homeRecommendsDto = homeRecommendsService.getHomeRecommendById(id);
        return beanMapper.map(homeRecommendsDto, HomeRecommendsVO.class);
    }

    @PatchMapping(value = "/{id}", consumes = ContentType.JSON, produces = ContentType.JSON)
    public Object updateHomeRecommend(@PathVariable("id") Integer id,
                                      @Valid @RequestBody HomeRecommendUpdateRequest updateRequest) {
        homeRecommendsService.updateHomeRecommendById(id, beanMapper.map(updateRequest, HomeRecommendsDto.class));
        return ok();
    }

    @DeleteMapping(value = "/{id}", consumes = ContentType.JSON, produces = ContentType.JSON)
    public Object deleteHomeRecommend(@PathVariable("id") Integer id) {
        homeRecommendsService.deleteById(id);
        return ok();
    }

}
